package com.alibaba.nacos.example.sentinel;

import java.util.List;
import java.util.Properties;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;

//@AutoConfigureAfter({ConfigService.class})

@Component
@EnableConfigurationProperties({NacosConfigProperties.class})
public class InitProperties implements InitializingBean{
	
	@NacosInjected
 	private ConfigService configService;
 	
	 @Autowired
		private NacosConfigProperties nacosConfigProperties;
	 
	 
	 final String dataId = "com.alibaba.nacos.demo.flow.rule";
    public  void updateKey() {
     	
 
       
//        final String remoteAddress = "47.74.227.13";
       final String groupId = nacosConfigProperties.getGroup();
       
        final String rule = "[\n"
            + "  {\n"
            + "    \"resource\": \"TestResource\",\n"
            + "    \"count\": 5.0\n"
            + "  }\n"
            + "]";
      //  ConfigService configService = NacosFactory.createConfigService(remoteAddress);
        try {
        	System.out.println("@AutoConfigureAfter==="+nacosConfigProperties.toString());
			configService.publishConfig(dataId, groupId, rule);
		} catch (NacosException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    //https://blog.csdn.net/Jaiaxn/article/details/107107170
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("------------afterPropertiesSet----------------r");
		
		updateKey();
		
		 //Properties properties
        ReadableDataSource<String, List<FlowRule>> flowRuleDataSource =
        		new NacosDataSource<>(nacosConfigProperties.getServerAddr(), nacosConfigProperties.getGroup(), dataId,
            source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
        FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
        
        // Assume we config: resource is `TestResource`, initial QPS threshold is 5.
		
	}
	
	public Properties getProperties(NacosConfigProperties nacosConfigProperties) {
		Properties ret=new Properties();
		return ret;
		//ret.setProperty(key, nacosConfigProperties.getAccessKey())
	}
}
